package com.example.mardan.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import com.example.mardan.adapter.BooksAdapter;
import com.example.mardan.contract.BookContract;
import com.example.mardan.entity.BookEntity;
import com.example.mardan.presenter.BookPresenter;
import com.example.read.databinding.ActivityBooksBinding;

import java.util.ArrayList;
import java.util.List;

public class BooksActivity extends AppCompatActivity implements BookContract.BookView {
    private ActivityBooksBinding binding ;
    private BooksAdapter adapter ;
    private List<BookEntity.ResultData.BookData> bookDataList ;
    private BookContract.BookPresenter presenter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBooksBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
    }

    @Override
    public void showData(BookEntity bookEntity) {
        bookDataList = bookEntity.getResult().getData();
        adapter.updateAdapter(bookDataList);
      //  Toast.makeText(this, bookDataList.get(0).getTitle(), Toast.LENGTH_SHORT).show();
    }

    private void init(){
        presenter = new BookPresenter(this,this);
        Intent intent = getIntent() ;
        String catalog_id = intent.getStringExtra("catalog_id");
        String title = intent.getStringExtra("title");
//        Toast.makeText(this, title, Toast.LENGTH_SHORT).show();
        //Toast.makeText(this, catalog_id, Toast.LENGTH_SHORT).show();
        binding.title.setText(title);
        assert catalog_id != null;
        presenter.getData(Integer.parseInt(catalog_id));
        bookDataList = new ArrayList<>();
        adapter = new BooksAdapter(bookDataList,this);
        binding.booksList.setLayoutManager(new GridLayoutManager(this,1));
        binding.booksList.setAdapter(adapter);
    }

    @Override
    public void Loading(boolean loading) {
        binding.bookLoading.loadingRep.setVisibility(loading?View.VISIBLE:View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {

    }

    @Override
    public void Complete() {
        binding.bookLoading.loadingRep.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }

}